package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ketan1 extends AppCompatActivity implements AdapterView.OnItemClickListener {

    Handler handler;
    private ListView mylist;
    private RateManager manager;
    float hv;
    String bz;
    View progressBar;
    private ArrayList<HashMap<String, String>> listItems;

    @SuppressLint({"MissingInflatedId", "WrongViewCast"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_ketan1);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        handler=new Handler(Looper.getMainLooper()){
            public void handleMessage(Message msg){
                if(msg.what==3){
                    List<String> list3 = (List<String>) msg.obj;
                    ListAdapter adapter2 = new ArrayAdapter<String>(ketan1.this,
                            android.R.layout.simple_expandable_list_item_1,list3);
                    mylist.setAdapter(adapter2);
                    progressBar.setVisibility(View.GONE);
                }
                super.handleMessage(msg);
            }
        };

        RateManager manager = new RateManager(this);
//        RateItem item = new RateItem("欧元2",34.567f);
//        manager.add(item);

        progressBar = findViewById(R.id.progressbar);

        ArrayList<HashMap<String,String>>listItems =new ArrayList<HashMap<String,String>>();
//        for (int i =0;i<10;i++){
//            HashMap<String,String>map = new HashMap<String,String>();
//            map.put("ItemTitle","Rate: "+i);
//            map.put("ItemDetail","detail"+i);
//            listItems.add(map);}


        Thread t = new Thread(()->{

            try {
            Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();

            Elements tables = doc.getElementsByTag("table");
            Element table =tables.get(1);
            Elements trs = table.getElementsByTag("tr");
            trs.remove(0);
            for (Element tr : trs) {
                Elements tds = tr.children();
                Element td1 = tds.first();
                Element td2 = tds.get(5);
                String str1 = td1.text();
                String str2 = td2.text();
                float s2 = Float.parseFloat(str2);
                RateItem item = new RateItem(str1,s2);
                manager.add(item);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("ItemTitle", ""+str1);
                map.put("ItemDetail", ""+str2);
                listItems.add(map);
            }

            Message msg = handler.obtainMessage(3,listItems);
            handler.sendMessage(msg);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        t.start();

        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,R.layout.list_item,
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.itemTitle,R.id.itemDetail});

        myadapter myAdapter=new myadapter(this,R.layout.list_item,listItems);

        mylist = findViewById(R.id.list2);
        mylist.setAdapter(myAdapter);
        mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Object itemAtPosition = mylist.getItemAtPosition(position);
        HashMap<String, String> map = (HashMap<String, String>) itemAtPosition;
        String titleStr = map.get("ItemTitle");
        String detailStr = map.get("ItemDetail");
        Intent intent = new Intent(this, zhexian.class);
        intent.putExtra("title",titleStr);
        intent.putExtra("detail",detailStr);

        startActivityForResult(intent,6);

    }
//    public boolean onItemLongClick(AdapterView<?> adapterView,View view,int position,long l){
//        adapter.remove(list2.getIteAtPosition(position));
//        return true;
//
//    }

}